//
//  BaseViewController.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/5.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,UIAlertViewDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
    if ([self.navigationController.viewControllers count] > 1 || self.presentingViewController != nil)
    {
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
        {
//            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//            self.navigationController.interactivePopGestureRecognizer.delegate = self;
        }
        self.leftBtn.hidden = NO;
        
        
    }
    self.page = 1;
    //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:nav_bg_color] forBarMetrics:UIBarMetricsDefault];
    //    self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:COLORRGB(255, 255, 255)}];
    // self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    DLog(@"height---%f--%f",[UINavigationBar appearance].bounds.size.height,self.navigationController.navigationBar.height);
}

//显示引导
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
- (UILabel*)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = 1;
        _titleLabel.font = [UIFont systemFontOfSize:17];
        self.navigationItem.titleView = _titleLabel;
    }
    
    return _titleLabel;
}

- (UIImageView*)tpTitleView{
    if (!_tpTitleView) {
        _tpTitleView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 27, 15)];
        _tpTitleView.image = ImageNamed(@"Media_video_title_icon");
        _tpTitleView.contentMode = UIViewContentModeScaleAspectFit;
        self.navigationItem.titleView = _tpTitleView;
    }
    
    return _tpTitleView;
}

#pragma mark --- 导航栏左侧按钮点击方法 ---
- (void)leftBtnAction:(UIButton *)sender
{
    if (self.navigationController.viewControllers.count == 1)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setIsBarWhite:(BOOL)isBarWhite{
    _isBarWhite = isBarWhite;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:isBarWhite ? [UIColor whiteColor] : TPAllBackgroundColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:isBarWhite ? TP_COLOR_240 : [UIColor clearColor]]];
    
    
    if(isBarWhite){
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    }else{
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    }
}

-(UIButton *)rightBtn
{
    if(!_rightBtn){
        //导航栏右侧按钮
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0, 50, 40);
        // _rightBtn = [UIButton buttonWithType:101];
        _rightBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_rightBtn addTarget:self
                      action:@selector(rightBtnAction:)
            forControlEvents:UIControlEventTouchUpInside];
        [_rightBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
//        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    }
    return _rightBtn;
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark --- 导航栏右侧按钮点击方法 ---
- (void)rightBtnAction:(id )sender
{
    //虚方法...
}

- (UIButton *)leftBtn{
    if(!_leftBtn){
        
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.frame = CGRectMake(0, 0,31, 30);
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_leftBtn setTitle:@"" forState:UIControlStateNormal];
        _leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _leftBtn.contentMode = UIViewContentModeScaleAspectFit;
                _leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -13.0f, 0, 0);
        //        _leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -45, 0, 0);
        
        [_leftBtn setImage:[UIImage imageNamed:@"Nav_left_arrow_black"] forState:0];
        
        [_leftBtn addTarget:self
                     action:@selector(leftBtnAction:)
           forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
        
    }
    
    return _leftBtn;
}

-(UIBarButtonItem *)rightBtnItem{
    if(!_rightBtnItem){
        
        _rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnAction:)];
        [_rightBtnItem setTintColor:[UIColor whiteColor]];
        [_rightBtnItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                               fontSystemOfSize(16), NSFontAttributeName,
                                               [UIColor whiteColor], NSForegroundColorAttributeName,
                                               nil] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem =_rightBtnItem;
    }
    return _rightBtnItem;
    
}

#pragma mark - Propertys

- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.tableViewStyle];
        _tableView.delegate = self;
        _tableView.backgroundColor = TP_COLOR_245;
        _tableView.dataSource = self;
        [_tableView setScrollsToTop:YES];
        _tableView.estimatedRowHeight = 300;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorColor = tableViewSeparatorColor;
        _tableView.tableFooterView = [[UIView alloc] init];
        if (![self validateSeparatorInset]) {
            if (self.tableViewStyle == UITableViewStyleGrouped) {
                UIView *backgroundView = [[UIView alloc] initWithFrame:_tableView.bounds];
                backgroundView.backgroundColor = _tableView.backgroundColor;
                _tableView.backgroundView = backgroundView;
            }
        }
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(IOS7_TOP_Y, 0, 49, 0);
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        } else {
            // Fallback on earlier versions
        }
        
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(IOS7_TOP_Y);
            make.left.right.bottom.mas_equalTo(0.0f);
        }];
    }
    return _tableView;
}


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return _dataSource;
}

#pragma mark - Life cycle


- (void)dealloc {
    //    self.dataSource = nil;
    //    self.tableView.delegate = nil;
    //    self.tableView.dataSource = nil;
    //    self.tableView = nil;
}
#pragma mark - TableView Helper Method
- (BOOL)validateSeparatorInset {
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        return YES;
    }
    return NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // in subClass
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}


//Tableview 分割线
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    //    {
    //        [cell setSeparatorInset:UIEdgeInsetsZero];
    //    }
    //    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {        [cell setLayoutMargins:UIEdgeInsetsZero];    }
    //    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
    //        [cell setPreservesSuperviewLayoutMargins:NO];
    //    }
}

-(void)refreshSccuess{
    if(self.page == 1){
        [self.tableView xw_endPullToRefresh];
    }else{
        [self.tableView xw_endInfinityRefreshAnimation:NO];
    }
    
    [self.tableView reloadData];
    
    
}
-(void)refreshFail{
    if(self.page == 1){
        [self.tableView xw_endPullToRefresh];
    }else{
        [self.tableView xw_endInfinityRefreshAnimation:NO];
    }
    
    if(self.page > 1)
        self.page --;
    
    [self.tableView reloadData];
}

@end
